package com.base.cn.platform.os.service.qa.record;

import com.base.cn.platform.os.common.mybatis.BaseBiz;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.action.ActionKey;
import com.base.cn.platform.os.dao.qa.record.QaBuyRecordDao;
import com.base.cn.platform.os.entity.qa.record.QaBuyRecord;
import com.base.cn.platform.os.entity.qa.record.QaTeacherCommission;
import com.base.cn.platform.os.service.manage.teacher.SysTeacherService;
import com.base.cn.platform.os.service.manage.user.integral.CusUserIntegralService;
import com.base.cn.platform.os.service.qa.question.QaQuestionBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 付费提问记录Biz
 * @author LIZHIPENG
 * @since 2018/5/2
 */
@Service
public class QaBuyRecordBiz extends BaseBiz<QaBuyRecord, QaBuyRecordDao> {

    @Autowired
    private QaQuestionBiz qaQuestionBiz;
    @Autowired
    private QaTeacherCommissionBiz qaTeacherCommissionBiz;
    @Autowired
    private SysTeacherService sysTeacherService;
    @Autowired
    private CusUserIntegralService cusUserIntegralService;

    /**
     * 添加问答支付记录
     * @param qaBuyRecordList
     */
    @Transactional(rollbackFor = IllegalArgumentException.class )
    public void createBuyRecord(List<QaBuyRecord> qaBuyRecordList){
        //同时更新问答状态
        String qaIds = qaBuyRecordList.stream().map(e->e.getQuestionId().toString()).distinct().collect(Collectors.joining(","));
        //支付成功后，设置状态为正常
        qaQuestionBiz.updateQuestionStatus(qaIds,1);

        //添加讲师分成记录数据
        List<QaTeacherCommission> commissionList = new ArrayList<>();
        String teacherIds = qaBuyRecordList.stream().filter(e-> DataUtil.idIsNotNull(e.getTeacherId())).map(e->e.getTeacherId().toString()).collect(Collectors.joining(","));
        Map<BigDecimal,Map<String,Object>> teacherMaps = sysTeacherService.findSysTeacherToMapByIds(teacherIds,false,false,false,false, false);
        qaBuyRecordList.forEach(e->{
            Map<String,Object> teacher = teacherMaps.get(e.getTeacherId());
            //分成比例
            Object _separateInto = teacher.get("separateInto");
            QaTeacherCommission commission = new QaTeacherCommission();
            commission.setQuestionId(e.getQuestionId());
            commission.setUserId(e.getUserId());
            commission.setTeacherId(e.getTeacherId());
            e.setTeacherId(null);
            commission.setOrderId(e.getOrderId());
            commission.setOrderNo(e.getOrderNo());
            commission.setDetailsId(e.getDetailsId());
            if(_separateInto!=null){
                double separateInto = Double.parseDouble(_separateInto.toString());
                commission.setScale(separateInto);
                //分成金额
                double price = e.getPrice()*(commission.getScale().doubleValue()/100D);
                commission.setPrice(price);
            }
            commission.setStatus(1);//未提取
            commissionList.add(commission);
        });
        qaTeacherCommissionBiz.addTeacherCommission(commissionList);
        //保存问答支付记录
        this.batchSave(qaBuyRecordList);
        //验证该提问者是否像该老师提过问
         boolean   userNum =false;
        BigDecimal count = qaQuestionBiz.count("userId=" + commissionList.get(0).getUserId() + " and teacherId=" + commissionList.get(0).getTeacherId());
        if(!DataUtil.idIsNotNull(count)){
            userNum = true;
        }
        //修改讲师问答量
        sysTeacherService.updateQuestionNumAndUserNum(commissionList.get(0).getTeacherId(),true,userNum);
    }
}
